package band.wukong.practice.mina.quickstart.sumup.codec;

import band.wukong.practice.mina.quickstart.sumup.message.ResultMessage;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

/**
 * Desc:
 * author: wukonggg@139.com
 */
public class ResultMessageEncoder<T extends ResultMessage> extends AbstractMessageEncoder<T> {

    public ResultMessageEncoder() {
        super(Constants.RESULT);
    }

    @Override
    protected void encodeBody(IoSession session, T message, IoBuffer out) {
        if (message.isOk()) {
            out.putShort((short) Constants.RESULT_OK);
            out.putInt(message.getValue());
        } else {
            out.putShort((short) Constants.RESULT_ERROR);
        }
    }

    public void dispose() throws Exception {
    }
}
